import { getUserinfo } from '@/api/api.js'
import { mapGetters } from "vuex";
import store from '@/store/index.js'
export default {
	data() {
		return { 
			userinfo:{}
		};
	},
	computed: mapGetters(['isLogin']),
	watch: {
		isLogin: {
			handler: function(newV, oldV) {
				if (newV) {
					this.getUserInfo()
				}
			},
			deep: true
		}
	},
	onShow(){
		let userInfo = store.getters.userinfo
		if(this.isLogin && !userInfo){
			this.getUserInfo();
		}else if(this.isLogin){
			this.userinfo = JSON.parse(userInfo)
		}
	},
	methods: {
		// 获取用户信息
		getUserInfo: function() {
			let _this = this
			return new Promise((reslove) => {
				getUserinfo().then(r => {
					_this.userinfo = r
					_this.$cache.set('userInfo', r)
					reslove()
				})
			}).catch((err) => {
				console.log('🐛🐛🐛',err);
			});
		}
	}
}